package com.foreverwin.jzymes.job;

import com.foreverwin.jzymes.common.mapper.ItemAdviceMapper;
import com.foreverwin.jzymes.framework.quartz.domain.SysJob;
import com.foreverwin.jzymes.framework.util.DateUtil;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.framework.util.SapUtil;
import com.sap.conn.jco.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class ItemDataSyncService {

    @Value("${mes.sap.activeDestination}")
    private String destination;

    @Autowired
    ItemAdviceMapper itemAdviceMapper;

    /**
     * 物料同步方法入口
     *
     * @param job
     */
    public void syncItemData( SysJob job ){
        String site = job.getSite();
        Map<String,String> paramMap = job.getJobParamMap();
        String revision = paramMap.get( "revision" );
        Assert.hasText( revision, I18nUtil.getI18nText("production.quartz.jobParamNotBlank", new String[]{ job.getJobName(), "revision" } ) );
        String startDate = paramMap.get( "startDate" );
        String endDate = paramMap.get( "endDate" );
        String currentDate = DateUtil.format( new Date(), "yyyyMMdd" );

        JCoDestination jcoDestination = SapUtil.getDestination( destination );
        JCoFunction jcoFunction = SapUtil.getFunction( destination, "ZMM_MATERIAL_DISPLAY" );

        JCoParameterList importParamlist = jcoFunction.getImportParameterList();

        try {

            JCoContext.begin( jcoDestination );

            importParamlist.setValue("LAEDA_FRM", StringUtils.isNotBlank( startDate ) ? startDate : currentDate  );
            importParamlist.setValue("LAEDA_TO", StringUtils.isNotBlank( endDate ) ? endDate : currentDate );
            System.out.println( importParamlist );

            jcoFunction.execute( jcoDestination );

            JCoTable exportTable = jcoFunction.getTableParameterList().getTable("ET_OUT");
            for( int i = 0; i < exportTable.getNumRows(); i++ ){
                exportTable.setRow( i );
                String item = exportTable.getString( "MATNR" ).replaceAll("^(0+)", "");
                String description = exportTable.getString( "MAKTX" );
                String itemType = exportTable.getString( "MTART" );
                String unit = exportTable.getString( "MEINS" );

                if( itemType.equals( "Z001" ) ){
                    itemType = "M";
                }else if( itemType.equals( "Z002" ) ){
                    itemType = "B";
                }else {
                    itemType = "P";
                }

                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put( "handle", "ItemBO:"+ site + "," + item + "," + revision );
                itemMap.put( "site", site );
                itemMap.put( "item", item );
                itemMap.put( "description", description );
                itemMap.put( "itemType", itemType );
                itemMap.put( "unit", unit );
                itemMap.put( "revision", revision );
                itemMap.put( "currentRevision", "true" );

                Map<String,Object> resultMap = itemAdviceMapper.selectItemCurrRevMap( site, item );
                if( resultMap != null ){
                    itemMap.put( "handle", resultMap.get("HANDLE") );
                    itemMap.put( "modifiedDateTime", new Date() );
                    itemAdviceMapper.updateItem( itemMap );
                }else{
                    itemMap.put( "statusBo", "StatusBO:"+ site +",201" );
                    itemMap.put( "lotSize", 999999999 );
                    itemMap.put( "quantityRestriction", "A" );
                    itemMap.put( "assyDataTypeBo", "DataTypeBO:"+ site +",ASSEMBLY,NONE" );
                    itemMap.put( "createdDateTime", new Date() );
                    itemMap.put( "modifiedDateTime", new Date() );
                    itemAdviceMapper.insertItem( itemMap );
                }
            }

            JCoContext.end( jcoDestination );
        } catch (JCoException e) {
            e.printStackTrace();
        }

    }
}
